<?php
class PNPersonArray extends PNObjectExArray {
  function PNPersonArray($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'votecanvasser_person';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';

    $this->_objJoin[]     = array ( 'join_table'  =>  'votecanvasser_type',
                            'join_field'          =>  array('id','name'),
                            'object_field_name'   =>  array('typ_id', 'typ_name'),
                            'compare_field_table' =>  'typ_id',
                            'compare_field_join'  =>  'id');
/*
    $this->_objJoin[]     = array ( 'join_table'  =>  'votedatacenter_district',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('dis_name'),
                            'compare_field_table' =>  'dis_id',
                            'compare_field_join'  =>  'id');

    $this->_objJoin[]     = array ( 'join_table'  =>  'votedatacenter_tambon',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('tam_name'),
                            'compare_field_table' =>  'tam_id',
                            'compare_field_join'  =>  'id');

    $this->_objJoin[]     = array ( 'join_table'  =>  'votedatacenter_village',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('vil_name'),
                            'compare_field_table' =>  'vil_id',
                            'compare_field_join'  =>  'id');

    $this->_objJoin[]     = array ( 'join_table'  =>  'votedatacenter_house_no',
                            'join_field'          =>  array('no', 'latitude', 'longitude'),
                            'object_field_name'   =>  array('hou_no', 'hou_latitude', 'hou_longitude'),
                            'compare_field_table' =>  'hou_id',
                            'compare_field_join'  =>  'id');

    $this->_objJoin[]     = array ( 'join_table'  =>  'votedatacenter_gender',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('gen_name'),
                            'compare_field_table' =>  'gen_id',
                            'compare_field_join'  =>  'id');

*/
    $this->_init($init, $where);
  }

  function genSort(){
    //return " ORDER BY pro_name ,dis_name ,tam_name ,vil_name ASC";
    $pntables = pnDBGetTables();
    $column = $pntables[$this->_objType.'_column'];

    $desc  = FormUtil::getPassedValue ('desc', null, 'GET');
    $asc  = FormUtil::getPassedValue ('asc', null, 'GET');
    
    if(isset($desc)){
      return " ORDER BY $column[$desc] desc";
    }
    if(isset($asc)){
      return " ORDER BY $column[$asc] asc";
    }
    return " ORDER BY pro_name, dis_name, tam_name, vil_id, hou_no, per_firstname ASC";
  }

  function genFilter(){
    $form  = FormUtil::getPassedValue ('form', false);
    $wheres = array();
    if ($form['pro_id'] && intval($form['pro_id'])) $wheres[] = " per_pro_id = ".$form['pro_id'];
    if ($form['dis_id'] && intval($form['dis_id'])) $wheres[] = " per_dis_id = ".$form['dis_id'];
    if ($form['tam_id'] && intval($form['tam_id'])) $wheres[] = " per_tam_id = ".$form['tam_id'];
    if ($form['vil_id'] && intval($form['vil_id'])) $wheres[] = " per_vil_id = ".$form['vil_id'];
    if ($form['hou_id'] && intval($form['hou_id'])) $wheres[] = " per_hou_id = ".$form['hou_id'];

    return implode(' AND ', $wheres) ;
  }
  function genFilterPersonByHouseId(){
    $pntables = pnDBGetTables();
    $column  = $pntables[$this->_objType.'_column'];
    
    $id = FormUtil::getPassedValue ('id', FALSE, 'GET');
    
    if(is_numeric($id)){

      $wheres[] = " $column[hou_id] = ".$id;
    }

    return implode(' AND ', $wheres) ;
  }
  function genFilterPersonByVillageId(){
    $pntables = pnDBGetTables();
    $column  = $pntables[$this->_objType.'_column'];
    
    $id = FormUtil::getPassedValue ('id', FALSE, 'GET');
    
    if(is_numeric($id)){

      $wheres[] = " $column[vil_id] = ".$id;
    }

    return implode(' AND ', $wheres) ;
  }
  function genFilterPersonByTambonId(){
    $pntables = pnDBGetTables();
    $column  = $pntables[$this->_objType.'_column'];
    
    $id = FormUtil::getPassedValue ('id', FALSE, 'GET');
    
    if(is_numeric($id)){

      $wheres[] = " $column[tam_id] = ".$id;
    }

    return implode(' AND ', $wheres) ;
  }
  function genFilterPersonByDistrictId(){
    $pntables = pnDBGetTables();
    $column  = $pntables[$this->_objType.'_column'];
    
    $id = FormUtil::getPassedValue ('id', FALSE, 'GET');
    
    if(is_numeric($id)){

      $wheres[] = " $column[dis_id] = ".$id;
    }

    return implode(' AND ', $wheres) ;
  }
  function genFilterPersonByProvinceId(){
    $pntables = pnDBGetTables();
    $column  = $pntables[$this->_objType.'_column'];
    
    $id = FormUtil::getPassedValue ('id', FALSE, 'GET');
    
    if(is_numeric($id)){

      $wheres[] = " $column[pro_id] = ".$id;
    }

    return implode(' AND ', $wheres) ;
  }
}
